// 搜索历史管理
import { useStore } from 'vuex';
import { save, remove, clear } from '../../assets/js/array-store';
import { SEARCH_KEY } from '../../assets/js/constant';

export default function useSearchHistory() {
  const maxLen = 200;
  const store = useStore();

  function saveSearch(query) {
    const searches = save(query, SEARCH_KEY, (item) => {
      return item === query;
    }, maxLen);
    store.commit('setSearchHistory', searches);
  }

  // 删除历史记录
  function deleteSearch(query) {
    const searches = remove(SEARCH_KEY, (item) => {
      return item === query;
    });
    store.commit('setSearchHistory', searches);
  }

  // 清空历史记录
  function clearSearch() {
    const searches = clear(SEARCH_KEY);
    store.commit('setSearchHistory', searches);
  }
  return {
    clearSearch,
    saveSearch,
    deleteSearch
  };
}
